package com.taru.wxhsh.api;

import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.controller.DetailController;
import com.taru.wxhsh.pojo.Detail;
import com.taru.wxhsh.service.DetailService;
import com.taru.wxhsh.util.UidGenerators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class DetailApi {
    Logger logger  = LoggerFactory.getLogger(DetailApi.class);
    @Autowired
    DetailService detailService;
    /**
     * WX加入购物车
     * @param detail
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/detail/join")
    public Object shopCartIn(Detail detail){
        JsonResult<String> result = null;
        try {
            detailService.insert(detail);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"加入购物车成功");
        }catch (Exception ex){
            logger.error("加入购物车异常:{}",ex.getMessage());
            ex.printStackTrace();
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"加入购物车异常",ex.getMessage());
        }
        return result;
    }


    /**
     * 移除购物车
     * @param detailId
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/detail/cartDel")
    public Object cartDel(String detailId){
        JsonResult<String> result = null;
        try {
            detailService.deleteByPrimaryKey(detailId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"移除购物车成功");
        }catch (Exception ex){
            logger.error("移除购物车异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"移除购物车异常");
        }
        return  result;
    }


    /**
     * 查询购物车列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/api/detail/qryCartList")
    public Object qryCartList(String uid) {
        JsonResult<List<Detail>> result = null;
        try {
            List<Detail> list = detailService.qryCartList(uid);
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_OK, "查询购物车成功", list);
        } catch (Exception ex) {
            logger.error("查询购物车异常：{}", ex.getMessage());
            result = new JsonResult< List<Detail>>(JResponseConstants.STATUS_ERROR, "查询购物车异常");
        }
        return result;
    }

}




